-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct Babel dependency behavior #5046
Correct Babel dependency behavior #5046
Conversation
@babel/plugin-transform-runtime will try to insert Before Babel: async function fn() {}
module.exports = fn; After Babel: import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
// ...
module.exports = fn; Mixing commonjs modules and es modules will cause webpack to generate invalid code. |
Hmm, thank you for the information @lixiaoyan. |
@Timer Set |
Awesome @lixiaoyan. Mind sending a PR changing this or do you not care for the git blame? |
Even prepped the diff for you: diff --git a/packages/react-scripts/config/webpack.config.dev.js b/packages/react-scripts/config/webpack.config.dev.js
index 12b8f8cf..0775b62a 100644
--- a/packages/react-scripts/config/webpack.config.dev.js
+++ b/packages/react-scripts/config/webpack.config.dev.js
@@ -277,6 +277,11 @@ module.exports = {
],
cacheDirectory: true,
highlightCode: true,
+ // Babel assumes ES Modules, which isn't safe until CommonJS
+ // dies. This changes the behavior to assume CommonJS unless
+ // an `import` or `export` is present in the file.
+ // https://github.com/webpack/webpack/issues/4039#issuecomment-419284940
+ sourceType: 'unambiguous',
},
},
],
diff --git a/packages/react-scripts/config/webpack.config.prod.js b/packages/react-scripts/config/webpack.config.prod.js
index 1e0fadbf..6c44af5c 100644
--- a/packages/react-scripts/config/webpack.config.prod.js
+++ b/packages/react-scripts/config/webpack.config.prod.js
@@ -306,6 +306,11 @@ module.exports = {
],
cacheDirectory: true,
highlightCode: true,
+ // Babel assumes ES Modules, which isn't safe until CommonJS
+ // dies. This changes the behavior to assume CommonJS unless
+ // an `import` or `export` is present in the file.
+ // https://github.com/webpack/webpack/issues/4039#issuecomment-419284940
+ sourceType: 'unambiguous',
},
},
], |
I think the best place to add this option is "babel-preset-react-app/dependencies.js" (it just works). |
Makes sense. PR merged. 🎉 Thank you! |
Babel was compiling async/generators incorrectly in dependency using just
preset-env
. We need to rewrite these to use the non-global regenerator and apply other tweaks from the application configuration.On a side note, it took me forever to figure out there was some bad actor in Yarn Workspaces/Babel that required me to
git clean -fdx && yarn install
every time I changeddependencies.js
. Troubleshooting this took ages because of that.